package com.xktpx.controller;

import com.xktpx.annotation.Login;
import com.xktpx.common.utils.R;
import com.xktpx.common.validator.ValidatorUtils;
import com.xktpx.form.LoginForm;
import com.xktpx.msg.ICaptchaApi;
import com.xktpx.msg.model.MsgReq;
import com.xktpx.result.BaseResult;
import com.xktpx.service.TokenService;
import com.xktpx.service.UserService;
import com.xktpx.tencentsms.TencentSmsSender;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.swagger.annotations.ApiParam;
import org.hibernate.validator.constraints.EAN;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

/**
 * @author ZengXiong
 * @Description 登陆接口
 * @Date 2018/11/23 10:51
 */
@RestController
@RequestMapping("/api")
@Api(tags = "登录接口")
public class ApiLoginController {
    @Autowired
    private UserService userService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private ICaptchaApi iCaptchaApi;
    @Autowired
    private TencentSmsSender tencentSmsSender;

    @PostMapping("/login")
    @ApiOperation("登录")
    public BaseResult login(@ApiParam("手机号码") @RequestParam("mobileNo") String  mobileNo,
                            @ApiParam("验证码") @RequestParam("msgCode") String  msgCode) {
        //表单校验
        LoginForm loginForm = new LoginForm();
        loginForm.setMobile(mobileNo);
        loginForm.setVerificationCode(msgCode);
        //用户登录
        Map<String, Object> map = userService.login(loginForm);

        //用户登录
        return BaseResult.success(map);
    }

    @PostMapping("/thirdLogin")
    @ApiOperation("第三方登录")
    public BaseResult thirdLogin(@ApiParam("微信openId") @RequestParam(value = "wxOpenId", required = false) String wxOpenId,
                                 @ApiParam("qq_openId") @RequestParam(value = "qqOpenId", required = false) String qqOpenId,
                                 @ApiParam("微博openId") @RequestParam(value = "weiboOpenId", required = false) String weiboOpenId) {
        //用户登录
        Map<String, Object> map = userService.thirdLogin(wxOpenId, qqOpenId, weiboOpenId);
        //用户登录
        return BaseResult.success(map);
    }

    @Login
    @PostMapping("/logout")
    @ApiOperation("退出")
    public R logout(@ApiIgnore @RequestParam("userId") long userId) {
        tokenService.expireToken(userId);
        return R.ok();
    }

    @ApiOperation("发送验证码")
    @GetMapping("/sendMsgCode")
    public BaseResult sendMsgCode(@ApiParam("手机号码") @RequestParam("mobileNo") String  mobileNo, @ApiParam("验证码业务类型：login(登录) or bind(绑定)") @RequestParam("msgType") String  msgType) {
        //表单校验
        MsgReq msgReq = new MsgReq();
        msgReq.setMobileNo(mobileNo);
        msgReq.setMsgType(msgType);
        String result = iCaptchaApi.send(msgReq);
        Map<String, Object> map = new HashMap<>(1);
        map.put("result", result);

        return BaseResult.success(map);
    }

    @PostMapping("/test")
    public R test() {
        List<String> list = new ArrayList<>();
        list.add("15925681009");
        List<String> submit = tencentSmsSender.submit(list, "你的确认码是：123", null);
        Map<String, Object> map = new HashMap<>();
        map.put("result", submit);
        return R.ok(map);
    }
}
